package com.smh.service.impl;

import com.smh.common.CommonResult;
import com.smh.entity.DeviceData;
import com.smh.mapper.DeviceDataMapper;
import com.smh.service.DeviceDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class DeviceDataServiceImpl implements DeviceDataService {

    @Autowired
    private DeviceDataMapper deviceDataMapper;
    @Override
    public CommonResult<ArrayList<DeviceData>> queryAll() {
        CommonResult<ArrayList<DeviceData>> cr = new CommonResult<>();
        ArrayList<DeviceData> list = null;
        try {
            list = deviceDataMapper.query();
            cr.setCode(0);
            cr.setCount(list.size());
            cr.setData(list);
            cr.setMsg("查询成功");
            return cr;
        } catch (Exception e) {
            cr.setCode(-1);
            cr.setMsg("查询失败");
            return cr;
        }

    }

    @Override
    public CommonResult<DeviceData> add(DeviceData deviceData) {
        CommonResult<DeviceData> cr = new CommonResult<>();
        try {
            int add = deviceDataMapper.insert(deviceData);
            cr.setCode(1);
            cr.setMsg("添加成功");
            cr.setData(deviceData);
            cr.setCount(add);
            return cr;
        } catch (Exception e) {
            cr.setCode(-1);
            cr.setMsg("插入失败");
            return cr;
        }
    }

    @Override
    public CommonResult<DeviceData> update(DeviceData deviceData) {
        CommonResult<DeviceData> cr = new CommonResult<>();

        try {
            int update = deviceDataMapper.updateById(deviceData);
            cr.setCode(1);
            cr.setMsg("修改成功");
            cr.setData(deviceData);
            cr.setCount(update);
            return cr;
        } catch (Exception e) {
            cr.setCode(-1);
            cr.setMsg("修改失败");
            return cr;
        }
    }

    @Override
    public CommonResult<DeviceData> delete(Integer id) {
        CommonResult<DeviceData> cr = new CommonResult<>();
        try {
            int delete = deviceDataMapper.deleteById(id);
            cr.setCode(1);
            cr.setMsg("删除成功");
            cr.setCount(delete);
            return cr;
        } catch (Exception e) {
            cr.setCode(-1);
            cr.setMsg("删除失败");
            return cr;
        }
    }

    @Override
    public CommonResult<DeviceData> queryById(Integer id) {
        CommonResult<DeviceData> cr = new CommonResult<>();
        try {
            DeviceData device = deviceDataMapper.selectById(id);
            cr.setCode(1);
            cr.setMsg("查询成功");
            cr.setCount(1);
            cr.setData(device);
            return cr;
        } catch (Exception e) {
            cr.setCode(-1);
            cr.setMsg("查询失败");
            return cr;
        }
    }
}
